<?php
/*
 *享元模式
 */
//定义抽象角色
interface operater{
    function operate();
}
//共享抽象角色
class gongXiangOperater implements operater{
    private $content;
    public function __construct($content)
    {
        $this->content = $content;
    }

    function operate()
    {
        echo "共享输出:".$this->content.PHP_EOL;
    }
}
//不共享抽象角色
class noXiangOperater implements operater{
    private $content;
    public function __construct($content)
    {
        $this->content = $content;
    }

    function operate()
    {
        echo "不共享输出:".$this->content.PHP_EOL;
    }
}
//共享工厂
class factory{
    private $array = [];
    function getOprater(string $content){
        if(!isset($this->array[$content])){
           $this->array[$content] = new gongXiangOperater($content);
        }
        return $this->array[$content];
    }
}
//调用共享
$factory = new factory();
$factory->getOprater("A")->operate();
$factory->getOprater("B")->operate();

$no = new noXiangOperater("A");
$no->operate();